/* Layer and layout routines.
 *
 * Copyright (c) 1993-2007 Leo Spiekman, Xaveer Leijtens
 *
 * This file is part of libMask.
 *
 * libMask is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your
 * option) any later version.
 *
 * libMask is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with libMask.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "mask.h"

/* Common file handle */
FILE *MASK;
int output_format = FORMAT_GDS;

int clayer = -1;

/* Remember the pen state that's set by pen_up() and pen_down(). */
int _penstate = PENDOWN; 

/* Store the sw_overlap and cw_overlap values. */
double sw_overlap = 0;
double cw_overlap = 0;


/* We have to allocate free sublayout numbers (DS cif definitions) and we
 * don't know before which ones are going to be used in the top-level
 * calls. So we start from 200 (pre-incremented from 199), which should
 * give plenty of calls, but it's still a kludge.
 */
int this_layout = MAIN_LAYOUT;
int sub_sublayout = 199;

struct Layer *players = layers; ///TYB

void setlayers(struct Layer *pl)
{
  players = pl;
}///TYB
  
struct Layer *getlayers(void)
{
  return players;
}///TYB


struct Layer *layernr(int n)
{
  struct Layer *layer;
  for (layer = players; layer->number > -1; layer++) {//TYB
  //for (layer = layers; layer->number > -1; layer++) {
    if (layer->number == n)
      return layer;
  }
  fprintf(stderr, "layernr: layer number %d not found\n", n);
  exit(1);
}

/* return the current layer */
int currentlayer(void)
{
  return clayer;
}

/* stop writing to the mask, but keep moving the pointer */
int pen_up()
{
  int state = _penstate;
  _penstate = PENUP;
  return state;
}

/* start writing to the mask */
int pen_down()
{
  int state = _penstate;
  _penstate = PENDOWN;
  return state;
}

/* return TRUE/FALSE if layer n should be written to */
int drawlayer(int n)
{
  return (layernr(n)->visible == VISIBLE) & (_penstate == PENDOWN);
}

/* return the layer accuracy epsilon */
double getlayer_accuracy(int n)
{
  return (layernr(n)->eps);
}

/* return the layer accuracy epsilon */
void setlayer_accuracy(int n, double eps)
{
  layernr(n)->eps = eps;
}

/* Get visibility for layer <num> */
int getlayer_visibility(int num)
{
  return (layernr(num)->visible);
}

/* Set visibility for layer <num> */
void setlayer_visibility(int num, int vis)
{
  struct Layer *layer;

  if (num == L_AllLayers)
    for (layer = players; layer->number > 0; layer++)  ///TYB
    //for (layer = layers; layer->number > 0; layer++)
      layer->visible = vis;
  else
    layernr(num)->visible = vis;
}

/* Get the overlap value with which straight waveguides are extended.
 */
double get_sw_overlap(void)
{
  return sw_overlap;
}

/* Set the overlap value with which straight waveguides are extended.
 * Default 0
 */
void set_sw_overlap(double ol)
{
  sw_overlap = ol;
}

/* Get the overlap value with which curved waveguides are extended.
 */
double get_cw_overlap(void)
{
  return cw_overlap;
}

/* Set the overlap value with which curved waveguides are extended.
 * Default 0
 */
void set_cw_overlap(double ol)
{
  cw_overlap = ol;
}

/* return layout n */
/*
struct layout *layoutnr(int n)
{
  struct layout *layout;

  for (layout = layouts;; layout++) {
    if (layout->number == n)
      return layout;
    if (layout->number == MAIN_LAYOUT) {
      fprintf(stderr, "layoutnr: layout number %d not found\n", n);
      exit(1);
    }
  }
}
*/

/* write layout information to file for layout n */
void output_layout(int n)
{
  /* Numbers > 100 are for layouts that don't require the standard header
   * and footer (currently only CIF "file" sublayouts).
   */

  /* this routine shouldn't be called anymore */
  fprintf(stderr, "Call to output_layout() shouldn't occur!\n");
  /**********************************
  if (n < 100)
    msk_sublayout_header(layoutnr(n)->name);
  layoutnr(n)->func();
  if (n < 100)
    msklayout_trailer();
  **********************************/
}
